package com.shd.edanpei.customer.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.ui.activity.ALLMessageActivity;
import com.shd.edanpei.base.StateBean;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.UserBean;
import com.shd.edanpei.ui.activity.OpinionActivity;
import com.shd.edanpei.ui.activity.SettingsActivity;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.image.LoadImageCtrl;
import com.shd.edanpei.ui.activity.UserInfoActivity;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/9/28.
 * yang-wen-wen
 */

public class CUserActivity extends BaseActivity {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    @BindView(R.id.iv_message)
    ImageView ivMessage;


    public final String serverPhone = "0571-85236567";


    @Override
    protected int setLayout() {
        return R.layout.fragment_user;
    }

    @Override
    protected BaseIPresenter bindPresenter() {
        return null;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        LoadImageCtrl loadImageCtrl = new LoadImageCtrl(this);

        UserBean userBean = DaoManager.getInstance().getUserBean();
        tvAccount.setText(userBean.getAccountNo());
        tvName.setText(userBean.getRealName());
        if (!TextUtils.isEmpty(userBean.getAvatarUrl())) {
            loadImageCtrl.getGlide().load(userBean.getAvatarUrl()).into(ivHead);
        }

        getUnRead();
    }


    public void getUnRead() {
        ApiCreator.getApi().notRead()
                .compose(RxTransformer.<StateBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<StateBean>() {
                    @Override
                    protected void onSuccess(StateBean data) {
                        if (data.isNewMessageNotRead()) {
                            ivMessage.setImageResource(R.drawable.ic_message_home);
                        } else {
                            ivMessage.setImageResource(R.drawable.ic_message3);
                        }
                    }
                });
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_message)
    void onMessage() {
        startActivity(ALLMessageActivity.class);
    }

    @OnClick(R.id.user_info)
    void onInfo() {
        startActivity(UserInfoActivity.class);
    }

    @OnClick(R.id.back)
    void close() {
        finish();
    }

    @OnClick(R.id.tv_order)
    void onOrder() {
        startActivity(COrderActivity.class);
    }

    @OnClick(R.id.tv_opinion)
    void onOpinion() {
        startActivity(OpinionActivity.class);
    }

    @OnClick(R.id.tv_service)
    void onService() {
//        DialogUtil.infoDialog(this, "联系客服", "客服电话：" + serverPhone, null, "拨号",
//                new DialogUtil.SureListener() {
//                    @Override
//                    public void sure() {
//                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + serverPhone));
//                        startActivity(intent);
//                    }
//                });
//
        DialogUtil.callService(this);
    }

    @OnClick(R.id.tv_setting)
    void onSetting() {
        startActivity(SettingsActivity.class);
    }
}
